package com.elong.qadev.dao.impl;

import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.elong.qadev.dao.TaskDaoI;
import com.elong.qadev.entity.TaskInfo;

/**
 * 
 * @ClassName: TaskDaoImpl
 * @Description: TODO(TASK表数据相关增删改查)
 * @author kangchen
 * @date 2015年9月28日
 *
 */

@Repository("taskDao")
public class TaskDaoImpl implements TaskDaoI {
	Logger logger = Logger.getLogger(TaskDaoImpl.class);
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public boolean addTask(TaskInfo ti) {
		sessionFactory.getCurrentSession().save(ti);
		return true;
	}

	@Override
	public boolean deleteTask(int id) {
//		TaskInfo t = new TaskInfo();
//		t.setId(id);
		TaskInfo tempT = getTaskInfoById(id);
		sessionFactory.getCurrentSession().delete(tempT);
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<TaskInfo> getAllTaskInfo() {
		ArrayList<TaskInfo> at = (ArrayList<TaskInfo>) sessionFactory
				.getCurrentSession().createQuery("from TaskInfo").list();
		return at;
	}

	@Override
	public boolean upupdateTaskInfo(TaskInfo ti) {
		sessionFactory.getCurrentSession().update(ti);
		return true;
	}

	@Override
	public TaskInfo getTaskInfoById(int id) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				TaskInfo.class);
		c.add(Restrictions.eq("id", id));
		logger.debug("获取的结果是否为空："+c.list().isEmpty());
		if(c.list().isEmpty()){
			return null;
		}
		TaskInfo h = (TaskInfo) c.list().get(0);
		return h;
	}

}
